package com.samuel.webflux;


import com.samuel.domain.User;
import com.samuel.repository.UserRePository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * @date:2017/11/27
 * @time: 19:47
 * @author:zhaoyang
 * @desc:
 **/
@Component
public class UserHandler {

    @Autowired
    private UserRePository userRePository;

    public Mono<ServerResponse> save(ServerRequest request){

        /*
        * Mono 对象返回 0或者1个对象
        *
        * Flux 对象 返回大有1个（List）
        * */


        //把User包在Mono里面
        Mono<User> userMono = request.bodyToMono(User.class);

        //map()  类似于 consevrt 转化
        Mono<Boolean> booleanMono =  userMono.map(userRePository::save);

        return ServerResponse.ok().body(booleanMono,Boolean.class);
    }

}
